// pages/qiye_jieshao/qiye_jieshao.js
const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
    data: {
        showaddress: '请选择',
        province: '请选择',
        provinceCode: '',
        cityCode: '',
        areaCode: '',
        bankOpName: '',
        bankNumber: '',
        bankName: '',
        aliAccount: '',
        idCardNum: '',
        contactName: '',
        contactPhone: '',
        pnum: '',
        userinfo: [],
        has: 0,
        status: 0,
        remark: '',
        region: ["请选择地址",'','']
    },

    onShow() {
        this.setData({
            userinfo: wx.getStorageSync('user')
        });
        this.getinfo();
    },

    showPicker(e) {
        const type = e.currentTarget.dataset.type;
        this.selectComponent(`#${type}`).show();
    },
    bindRegionChange: function (e) {
        var value = e.detail.value; 
        var code = e.detail.code; 
        var postcode = e.detail.postcode;
        this.setData({
            region: e.detail.value,
            province: value[0],
            provinceCode: code[0],
            city: value[1],
            cityCode: code[1],
            area: value[2],
            areaCode: code[2],
        })
    },

    getinfo() {
        const that = this;
        util.httpRrquest(app.data.apiurl + 'shop/getMyArea', 'GET', {})
        .then(function (response) {
            var data = response.data.data;
            if (data.has == 1) {
                var data = data.data;
                that.setData({
                    contactPhone: data.contactPhone,
                    showaddress: data.province + data.city + data.area,
                    bankOpName: data.bankOpName,
                    bankNumber: data.bankNumber,
                    pnum: data.topshopnum,
                    bankName: data.bankName,
                    aliAccount: data.aliAccount,
                    idCardNum: data.idCardNum,
                    contactName: data.contactName,
                    status: data.status,
                    province: data.province,
                    provinceCode: data.provinceCode,
                    city: data.city,
                    cityCode: data.cityCode,
                    area: data.area,
                    areaCode: data.areaCode,
                    remark: data.remarks ? data.remarks : '无',
                    id: data.id
                });
            }
        }, function (error) {
            console.log(error);
        })
    },

    guanli() {
        wx.navigateTo({
            url: '/pages/areaPartnerAdmin/areaPartnerAdmin?id=' + this.data.id
        });
    },

    subinfo() {
        const that = this;
        if (!this.data.contactName) {
            wx.showToast({
                icon: 'none',
                title: '请填写申请人'
            });
            return;
        }
        if (!this.data.contactPhone) {
            wx.showToast({
                icon: 'none',
                title: '请填写联系电话'
            });
            return;
        }
        if (!this.data.bankOpName) {
            wx.showToast({
                icon: 'none',
                title: '请填写开户行'
            });
            return;
        }
        if (!this.data.bankName) {
            wx.showToast({
                icon: 'none',
                title: '请填写银行名称'
            });
            return;
        }
        if (!this.data.bankNumber) {
            wx.showToast({
                icon: 'none',
                title: '请填写银行卡号'
            });
            return;
        }
        if (!this.data.aliAccount) {
            wx.showToast({
                icon: 'none',
                title: '请填写支付宝账号'
            });
            return;
        }
        if (!this.data.idCardNum) {
            wx.showToast({
                icon: 'none',
                title: '请填写身份证号'
            });
            return;
        }
        if (!this.data.pnum) {
            wx.showToast({
                icon: 'none',
                title: '请填写上级编号'
            });
            return;
        }

        wx.showModal({
            title: '提示',
            content: '确认提交申请吗？',
            success(res) {
                if (res.confirm) {
                    var postdata = {
                        bankOpName: that.data.bankOpName,
                        bankNumber: that.data.bankNumber,
                        bankName: that.data.bankName,
                        aliAccount: that.data.aliAccount,
                        idCardNum: that.data.idCardNum,
                        contactName: that.data.contactName,
                        pnum: that.data.pnum,
                        contactPhone: that.data.contactPhone,
                        province: that.data.province,
                        city: that.data.city,
                        area: that.data.area,
                        provinceCode: that.data.provinceCode,
                        cityCode: that.data.cityCode,
                        areaCode: that.data.areaCode
                    }
                    util.httpRrquest(app.data.apiurl + 'shop/addAreaApply', 'GET', postdata)
                    .then(function (response) {
                        if (response.data.code == -1) {
                            wx.showToast({
                                icon: 'none',
                                title: response.data.msg
                            });
                        } else {
                            wx.showToast({
                                icon: 'none',
                                title: '提交成功，请耐心等待审核！'
                            });
                            setTimeout(() => {
                                wx.navigateBack();
                            }, 1500);
                        }
                        
                    }, function (error) {
                        console.log(error);
                    });
                }
            }
        });
    },

    inputContactName(e) {
        this.setData({
            contactName: e.detail.value
        });
    },

    inputContactPhone(e) {
        this.setData({
            contactPhone: e.detail.value
        });
    },

    inputIdCardNum(e) {
        this.setData({
            idCardNum: e.detail.value
        });
    },

    inputAliAccount(e) {
        this.setData({
            aliAccount: e.detail.value
        });
    },

    inputBankName(e) {
        this.setData({
            bankName: e.detail.value
        });
    },

    inputBankNumber(e) {
        this.setData({
            bankNumber: e.detail.value
        });
    },

    inputBankOpName(e) {
        this.setData({
            bankOpName: e.detail.value
        });
    },

    inputPnum(e) {
        this.setData({
            pnum: e.detail.value
        });
    }
});